import random
from pymt import *
from pyglet.gl import *



class ConnectionGesture(MTGestureWidget):
    #initializes the gestures that can be recognized
    def init_gdb (self):
        gdb = GestureDatabase()
     
        #A (without cross-bar)
        g = gdb.str_to_gesture('eNq1lttu00AQQN/9I+0L1dwvPwCvSP0AxCWqKgq1miDB3zNeJ7a5lC0PTqImqXfOxnN2Z+f6fvzx5XRzdzievj0dhjfn9xGG608jDrdXx9PT4+fD8WoYabh+GHm4/mvEbRs2jDLFacWNj/dfT1OYTWH+TNjbadQwxhSVFfWjAhCG16/ghl2ckJIxU1U4huPt1ffpOrbrBmqU4c6WAOY6HD+8/+csSO2meLg7T7HAwRIx1Yfj3ZmuzkCIgY4WTEl9ert11JXOEYpOnkFoXNdXuoAgSRiYmCZEn26N7gsd6xdSZsxzBPNKl7RkIdK6JUg279ObAMyFDg7sLOaIpOkhW7zEf6aG2moinPFFJxclwyxuTmnaJL6+1X8u6YfAPr1pJd6J3rSSXujIhhLkBOjBFraBU4hqmjCp1ue+VWpWyfeBN6mUFzhpZSYcLACkWLjCqaZyZp13lHEfzk0p4z7wZpQXo6z1rO1pbGpW7jZwiEhwmVe8UH+tcxPKug+8CeVFaKUiuSjgyFKwjVB0iKpw5+RUGejDm1BehEpwurDG/FdzA1dRqa05T1E1sguXJlQWoSqJiX7ROq23BU64rS+RfXgTKotQg2dzjgANK16DsKpyH96Eiq5pSYFEQQ2GOjg22x9+r7v9/S/NqPhO9KZUFqVct2/IpS8Nqu5eyu4ER4QkZTB11xcY1WZUcRd2E6qLUHT562qpK4heRY2sZowa0/epcw+hu7CbTfVd2M2lZvccKniQVK0HiMQwesHetybTcB94s2lrY1SLYT74VZwD1lVeNVFq9xtBariiZL+aW/NpuhO9GbVNY+Q5t3MUptOyXuFpKlKlx1zEUfuNizWllrvAvSl1XBtGyd873jO8qExmUqkCpDrBuuxm1PutbrGryfsj5dPr9ur0+HB4ev/146G6bpe2tuDXB05N+fknvBufHj99+3iaBuvwWm/q4FSvZqtaVKiSOYFvfgJuKVKI')
        g.label = 'A (no cross-bar)'
        gdb.add_gesture(g)
            
        self.gdb = gdb
    
    #constructor
    def __init__(self):
        super(ConnectionGesture, self).__init__()
        self.temp_gestures = {}
        #self.real_gestures = []
        self.real_gestures = {}
        self.init_gdb()
        #self.w=w
    
    
    def on_gesture(self, gesture, x, y):
        touchID = gesture.touchID
        
        best = self.gdb.find(gesture, minscore=0.8)
        
        if not best:
            print 'No gesture found\nString version of your last gesture :\n', self.gdb.gesture_to_str(gesture)
            #return None
        else:
            print 'Gesture found, score', best[0], ':', best[1].label
            result=best[1].label
            
            #connection
            if result == 'A (no cross-bar)' :    
                # finds the first and the last gesture's point
                p1x, p1y=self.points[touchID][0]
                p2x, p2y=self.points[touchID][len(self.points[touchID])-1]
                # for each wire, check if theres it intersects the line formed by the above points
                removed_wires=[]
                w = self.get_parent_window()
                for wire in w.real_connections:
                    origX, origY = wire.orig.calculateCenter()
                    destX, destY = wire.dest.calculateCenter()
                    #if yep, remove this wire
                    if self.intersect((p1x, p1y), (p2x, p2y), (origX, origY), (destX, destY)):
                        removed_wires.append(wire)
                for wire in removed_wires:
                    w.real_connections.remove(wire)
                    
                    
    #algoritm found here http://www.bryceboe.com/2006/10/23/line-segment-intersection-algorithm/
    def ccw(self, A,B,C):
        Ax, Ay = A
        Bx, By = B
        Cx, Cy = C
        
        return (Cy-Ay)*(Bx-Ax) > (By-Ay)*(Cx-Ax)
    
    def intersect(self, A,B,C,D):
        return self.ccw(A,C,D) != self.ccw(B,C,D) and self.ccw(A,B,C) != self.ccw(A,B,D)
        